<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'bas:cus:coreKhjgb:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsBasCusCoreKhjgbForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { CoreKhjgb, coreKhjgbSave, coreKhjgbForm } from '/@/api/bas/cus/coreKhjgb';
  import { coreYwlxTreeData, coreYwlxForm } from '/@/api/bas/ywlx/coreYwlx';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('bas.cus.coreKhjgb');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<CoreKhjgb>({} as CoreKhjgb);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增客户价格表') : t('编辑客户价格表'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('用车单位'),
      field: 'khjgbYcdw',
      fieldLabel: 'basCus.khdaDwqc',
      component: 'ListSelect',
      componentProps: {
        selectType: 'basCusSelect',
      },
      required: true,
      colProps: { lg: 12, md: 24 },
    },
    {
      label: t('车辆类型'),
      field: 'khjgbCllx',
      component: 'Select',
      componentProps: {
        dictType: 'car_type',
        allowClear: true,
      },
      required: true,
      colProps: { lg: 12, md: 24 },
    },
    {
      label: t('租赁方式'),
      field: 'khjgbZlfs',
      component: 'Select',
      componentProps: {
        dictType: 'car_zlfs',
        allowClear: true,
      },
      required: true,
    },
    {
      label: t('业务类型'),
      field: 'khjgbYwlx',
      fieldLabel: 'coreYwlx.ywlxLxmc',
      component: 'TreeSelect',
      componentProps: ({ formModel }) => {
        return {
          api: coreYwlxTreeData,
          onChange: (value: string) => {
            coreYwlxForm({ id: value }).then((result) => {
              formModel.khjgbJcgl = result?.coreYwlx?.ywlxJclc;
              formModel.khjgbJcsz = result?.coreYwlx?.ywlxJcsz;
            });
          },
        };
      },
      required: true,
    },
    {
      label: t('基础价'),
      field: 'khjgbJcj',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('基础公里'),
      field: 'khjgbJcgl',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('基础时长'),
      field: 'khjgbJcsz',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('超公里单价'),
      field: 'khjgbCgldj',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('超时单价'),
      field: 'khjgbCsdj',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('生效日期'),
      field: 'khjgbValid',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('失效日期'),
      field: 'khjgbInvalid',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('备注'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 600,
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('附件上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'coreKhjgb_file',
        uploadType: 'all',
      },
      colProps: { lg: 24, md: 24 },
      ifShow: false,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 8, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await coreKhjgbForm(data);
    record.value = (res.coreKhjgb || {}) as CoreKhjgb;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await coreKhjgbSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
